<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
<button id="get">get请求</button>
<button id="post">post请求</button>
<button id="delete">delete请求</button>
<button id="put">put请求</button>
</body>
<script>

let getBtn=document.querySelector(`#get`)
let postBtn=document.querySelector(`#post`)
let deleteBtn=document.querySelector(`#delete`)
let putBtn=document.querySelector(`#put`)


//实现get请求
getBtn.onclick=function(){ 

  axios({url:"/get1",params:"001",data:{name:"xiaoA",age:18}})

}

//实现post请求
postBtn.onclick=function(){ 

// axios({url:"/get1",params:"001",data:{name:"xiaoA",age:18}})

}





//配置对象
function axios ({url,method="get",params={},data={}}){

  return new Promise((resolve,reject)=>{

    //处理url参数
    // /get1?params=001
    
    //自己写的
    // url+="?"
    // url+="params"
    // url+="="
    // url+=params

    //别人写的
    // let quertString=''
    // Object.keys(params).forEach(key=>{
    //   quertString+=`${key}=$params[key]&`
    // })

    // if(quertString){
    //   quertString=quertString.substring(0,quertString.length-1)

    //   url+='?'+quertString
    // }



    console.log(url);
    //1.发送请求
    //怎么发请求
    const xhr =new XMLHttpRequest() 
    xhr.open(method,url,true)
    xhr.send()
    xhr.onreadystatechange=function(){

      if(xhr.readyState===4&&xhr.status>=200&&xhr.status<=299){

        
        console.dir(xhr.responseText);
        console.dir(JSON.parse(xhr.responseText));
        // console.log(url);
      }

    }
    //2.1成功怎么样
    resolve()
    //2.2失败怎么样
    reject()

  })

}

// axios({url:"/user"})


</script>
</html>